package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 新增菜品和口味
     * @param dishDTO
     * @return
     */
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //1. 拷贝属性
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //2. 向数据库中插入一条菜品数据
        dishMapper.insert(dish);

        //3. 获取插入口味集合的主键值
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()!=0){

            flavors.forEach((flavor)->{
                flavor.setDishId(dishId);
            });
            //3. 向数据库中插入多条口味数据
            dishFlavorMapper.insert(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //构建查询条件 limit
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page= dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            Dish dish=dishMapper.getById(id);
            //1. 判断当前菜品是否能够删除---是否是起售中的菜品
            if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //2. 判断当前菜品是否能够删除---是否被套餐关联？
        List<Long> meals=setMealDishMapper.getByDishIds(ids);
        if (meals!=null||meals.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3. 删除菜品表中的菜品数据
        /*for (Long id : ids) {
            dishMapper.deleteById(id);
            //4. 删除菜品关联的口味数据
            dishFlavorMapper.deleteByDishId(id);
        }*/

        //3. 批量删除菜品表中的菜品数据
        dishMapper.deleteByIds(ids);
        //4. 批量删除菜品关联的口味数据
        dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 根据id查询菜品
     * @param dishId
     * @return
     */
    @Transactional
    public DishVO getById(Long dishId) {
        //1. 根据id查询菜品菜品数据
        Dish dish = dishMapper.getById(dishId);
        //2. 根据id查询口味数据
        List<DishFlavor> dishFlavor=dishFlavorMapper.getByDishId(dishId);
        //3. 封装成DishVO对象
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);

        //4. 返回DishVO对象
        return dishVO;
    }

    /**
     * 更新菜品数据
     * @param dishDTO
     */
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        //拷贝菜品数据并保存
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //批量删除新增口味数据
        List<DishFlavor> flavors=dishDTO.getFlavors();
        Long dishId = dishDTO.getId();
        dishFlavorMapper.deleteByDishId(dishId);
        if (flavors.size() != 0){
            //因为之前这个菜皮可能没有口味，所以需要设置下dishId
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            //这里有个坑，如果更新菜品口味的话，之前的菜品口味是不会被删除的，所以需要先删除，再插入
            //dishFlavorMapper.update(flavors);
            //插入菜皮口味数据
            dishFlavorMapper.insert(flavors);
        }
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /**
     * 菜品的起售停售
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.startOrStop(status,id);
    }

    /**
     * 根据分类id查询菜品
     * @param id
     * @return
     */
    public List<Dish> getByCategoryId(Long id) {
        //根据分类id和起售状态查询菜品
        Dish dish = Dish.builder()
                .categoryId(id)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> dishList = dishMapper.getByCategoryId(dish);
        return dishList;
    }
}
